package com.itcast.googleplay.holder;

import com.itcast.googleplay.R;
import com.itcast.googleplay.domain.AppInfo;
import com.itcast.googleplay.http.CZApi;
import com.itcast.googleplay.utils.UIUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import android.text.format.Formatter;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class HomeHolder extends BaseHolder<AppInfo>{

	private TextView tvName,tvSize,tvDes;
	private ImageView ivIcon,ivDownload;
	private RatingBar ratingStars;
	private DisplayImageOptions options;

	@Override
	public void initData() {
		AppInfo appInfo = getData();
		
		//设置星星个数,支持0.5个
		ratingStars.setRating(appInfo.stars);
		tvName.setText(appInfo.name);
//		tvSize.setText(int)
//		tvSize.setText("字符串");
		tvSize.setText(Formatter.formatFileSize(UIUtils.getContext(), appInfo.size));
		tvDes.setText(appInfo.des);
		
		//ImageLoader   BitmapUtils	没有 三级缓存机制 lruCache	google  baidu   github  开源中国
		//一,内存缓存 lruCache:最近最少使用算法		
		//二,磁盘文件缓存
		//三,网络获取
		
		//初始图片缓存方式代码
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_default)//在加载图片的时候,展示的默认图片
				
				.showImageForEmptyUri(R.drawable.ic_default)//uri传递出现问题时展示的图片
				
				.showImageOnFail(R.drawable.ic_default)//加载网络失败的时候,展示的图片
				
				.cacheInMemory(true)//是否需要内存缓存
				
				.cacheOnDisk(true)//是否需要磁盘缓存
				
				.considerExifParams(true)//是否支持图片自动调整,让其正常图像显示
		
				.build();//设置圆角半径
		
//				.displayer(new RoundedBitmapDisplayer(20)).build();//设置圆角半径
				
		
		//参数一:图片链接地址
		//参数二:显示图片的空间
		//参数三:图片缓存配置(内存,磁盘,圆角)
		//http://127.0.0.1:8090/image?name=app/com.youyuan.yyhl/icon.jpg;
		ImageLoader.getInstance().displayImage(CZApi.BASEIMGURL+appInfo.iconUrl, ivIcon, options);
	}

	@Override
	public View initView() {
		View view = UIUtils.inflate(R.layout.list_item);
		//查找控件,在initData中给其赋值
		 ivIcon = (ImageView) view.findViewById(R.id.iv_icon);
		 ivDownload = (ImageView)view.findViewById(R.id.iv_download);
		
		 ratingStars = (RatingBar) view.findViewById(R.id.rating_stars);
		
		 tvName = (TextView) view.findViewById(R.id.tv_name);
		 tvSize = (TextView)view.findViewById(R.id.tv_size);
		 tvDes = (TextView)view.findViewById(R.id.tv_des);
		
		return view;
	}

}
